package cn.tradewin.advisor.pages.master;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.slf4j.Logger;

import cn.tradewin.advisor.commons.selectmodel.CodeLabelSelectModel;
import cn.tradewin.advisor.exceptions.DBException;
import cn.tradewin.advisor.persist.model.Department;
import cn.tradewin.advisor.persist.services.AccountService;
import cn.tradewin.advisor.persist.services.DeptService;

public class DeptUpd {

	private String id;
	
	@Property
	private Department dept;

	@Property
	private SelectModel deptOwnerModel;
	
	@Inject
	private AccountService accountService;
	
	@Inject
	private DeptService deptService;
	
	@Inject
	private Logger log;
	
	void onActivate(String id) throws DBException {
		this.id = id;
	}

	String onPassivate() {
		return id;
	}

	void onPrepareForRender() {
		dept = deptService.getDept(id);
		deptOwnerModel = new CodeLabelSelectModel(accountService.getAccountsOptions());
	}
	
	void onPrepare() {
		if (dept == null) {
			dept = new Department();
		}
		
	}

	Object onBack() {
		return DeptBoard.class;
	}

	@Log
	Object onSubmit() {
		log.info("**************************");
		log.info("dept id = " + dept.getId());
		log.info("**************************");
		deptService.updDept(dept);
		return DeptBoard.class;
	}
}